﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using Interactive.Utility.Extension;
using log4net;

namespace Interactive.Utility{
    public sealed class ConfigUtil{
        /// <summary>
        /// 配置项
        /// </summary>
        private readonly NameValueCollection _config;


        /// <summary>
        /// 是否合法
        /// </summary>
        private readonly bool _isLegal = true;

        private readonly ILog _myLog = LogManager.GetLogger(typeof (ConfigUtil));

        /// <summary>
        /// 构造函数,返回相应项的配置文件
        /// </summary>
        /// <param name="configKey"></param>
        public ConfigUtil(string configKey){
            _config = ConfigurationManager.GetSection(configKey) as NameValueCollection;
            if (_config == null){
                _myLog.Error("获取Interactive.Utility配置项时出错!configKey:"+configKey);
                _isLegal = false;
            }
        }

        /// <summary>
        /// 默认构造函数,默认返回Interactive.Utility的配置项
        /// </summary>
        public ConfigUtil() : this("Interactive.Utility"){
        }


        public string this[string key]{
            get { return GetStringValue(key); }
        }

        #region string 版本

        /// <summary>
        /// 获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public string GetStringValue(string key, string defaultValue){
            return GetStringValue(key, defaultValue, false);
        }

        /// <summary>
        ///  获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="isThrowExpection">当值为空时是否抛出异常</param>
        /// <returns>值</returns>
        public string GetStringValue(string key, bool isThrowExpection){
            return GetStringValue(key, "", isThrowExpection);
        }

        /// <summary>
        /// 获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public string GetStringValue(string key){
            return GetStringValue(key, "", false);
        }

        /// <summary>
        /// 获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="isThrowExpection">当值为空时是否抛出异常</param>
        /// <returns>值</returns>
        public string GetStringValue(string key, string defaultValue, bool isThrowExpection){
            CheckLegal(key);

            string value = _config.Get(key);
            if (string.IsNullOrEmpty(value)){
                if (isThrowExpection)
                    throw new Exception("不能从key:" + key + "得到值");
                return defaultValue;
            }

            return value;
        }

        #endregion

        #region int版本

        /// <summary>
        /// 获取配置项的值,int
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public int GetIntValue(string key, int defaultValue){
            return GetIntValue(key, 0, false);
        }

        /// <summary>
        /// 获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="isThrowExpection">当值为空时是否抛出异常</param>
        /// <returns>值</returns>
        public int GetIntValue(string key, bool isThrowExpection){
            return GetIntValue(key, 0, isThrowExpection);
        }

        /// <summary>
        /// 获取配置项的值,string
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public int GetIntValue(string key){
            return GetIntValue(key, 0, false);
        }

        /// <summary>
        /// 获取配置项的值,int
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="isThrowExpection">当值为空时是否抛出异常</param>
        /// <returns>值</returns>
        public int GetIntValue(string key, int defaultValue, bool isThrowExpection){
            CheckLegal(key);

            string value = _config.Get(key);
            if (string.IsNullOrEmpty(value)){
                if (isThrowExpection)
                    throw new Exception("不能从key:" + key + "得到值");

                return defaultValue;
            }

            return value.AsInt(defaultValue);
        }

        #endregion

        /// <summary>
        /// 检验是否合法
        /// </summary>
        /// <param name="key">键</param>
        private void CheckLegal(string key){
            if (!_isLegal)
                throw new Exception("pm3的配置项为空,请检查配置文件!");

            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("key为null或空!");
        }
    }
}